﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;

public partial class Manager_Topic_Topic : System.Web.UI.Page
{
    protected string pDel_mk = "", pUpd_mk = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        clsGetMenuAuth clsAuth = new clsGetMenuAuth(Session["AdminUserID"].ToString(), Session["AutID"].ToString(), Session["IsAdmin"].ToString(), Request.QueryString["CodeID"]);

        if (clsAuth.IsDeleteAuth() == true)
            pDel_mk = "Y";
        if (clsAuth.IsUpdateAuth() == true)
            pUpd_mk = "Y";
        if (!IsPostBack)
        {
            if (clsAuth.IsAddAuth() == false)
                btnAdd_Catalogue.Visible = false;
            txtTenChuDe.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('" + btnSearch.ClientID + "').click();return false;}} else {return true}; "); 
            BinData();
        }
        else
        {
            if (hPccMsg.Value != "" && hPccMsg.Value != null)
            {
                string strXML = "<PccMsg>" + hPccMsg.Value + "</PccMsg>";
                PccMsg msg = new PccMsg(strXML);
                string Submit = msg.Query("Method");
                if (Submit == "Refesh")
                {
                    BinData();
                    //Response.Redirect(Request.UrlReferrer.AbsoluteUri.ToString());
                    hPccMsg.Value = "";

                }
            }
        }
    }
    protected void btnAdd_Catalogue_Click(object sender, EventArgs e)
    {
        Response.Redirect("AddTopic.aspx?CodeID=" + Request.QueryString["CodeID"]);
    }

    private void BinData()
    {
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("TopicName", txtTenChuDe.Text.Trim());

        clsWebShop cls = new clsWebShop();
        DataSet ds = null;
        ds = cls.Get_TopicByCondition(msg.GetXmlStr);
        grv_Master.DataSource = ds;
        grv_Master.DataBind();

    }
    protected void grv_Master_RowCreated(object sender, GridViewRowEventArgs e)
    {
         /*for(int i= 0 ; i< grv_Master.Rows.Count - 1;i++)
         {
             System.Web.UI.WebControls.WebControl myControl=grv_Master.Rows[i].Cells[1];
             myControl.Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
         }*/
            
        
    }
    protected void grv_Master_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[0].ColumnSpan = 2;
            e.Row.Cells.RemoveAt(1);
        }

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Attributes.Add("onclick", "if(confirm('Delete???')) return true; return false;");
            //Truong hop xoa
            if (pDel_mk != "Y")
            {
                e.Row.Cells[1].Text = "";
                e.Row.Cells[1].Attributes.Add("onclick", "javascript:return false;");
            }
            //Truong hop Update
            if (pUpd_mk != "Y")
            {
                e.Row.Cells[0].Text = "";
                e.Row.Cells[0].Attributes.Add("onclick", "javascript:return false;");
            }
            if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState)5)
            {
                CheckBox chkEditChose = new CheckBox();
                chkEditChose = (CheckBox)e.Row.FindControl("chkEditChose");
                if (DataBinder.Eval(e.Row.DataItem, "ShowMK").ToString() == "Y")
                    chkEditChose.Checked = true;
                else
                    chkEditChose.Checked = false;
            }
            else if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                CheckBox chkViewChose = new CheckBox();
                chkViewChose = (CheckBox)e.Row.FindControl("chkViewChose");
                if (DataBinder.Eval(e.Row.DataItem, "ShowMK").ToString() == "Y")
                    chkViewChose.Checked = true;
                else
                    chkViewChose.Checked = false;


            }
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BinData();
    }
    protected void grv_Master_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grv_Master.EditIndex = e.NewEditIndex;
        BinData();
    }
    protected void grv_Master_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grv_Master.EditIndex = -1;
        BinData();
    }
    protected void grv_Master_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox txtTL=new TextBox();
        CheckBox chkEditChose; 
        chkEditChose = (CheckBox)grv_Master.Rows[e.RowIndex].FindControl("chkEditChose");
        txtTL = (TextBox)grv_Master.Rows[e.RowIndex].FindControl("txtETenTL");

        string state= "N";
        if (chkEditChose.Checked == true)
            state = "C";
       

         PccMsg msg = new PccMsg();
        string strShowMK = "N";
        if (chkEditChose.Checked == true)
            strShowMK = "Y";
         msg.CreateFirstNode("TopicID", grv_Master.DataKeys[e.RowIndex].Value.ToString());
        msg.CreateFirstNode("TopicName", txtTL.Text.ToString());
        msg.CreateFirstNode("ShowMK", strShowMK);
        msg.CreateFirstNode("Status", "N");
        msg.CreateFirstNode("UpdID","0");

        clsWebShop cls = new clsWebShop();
        cls.Upd_Topic(msg.GetXmlStr.ToString());


      //  ClientScript.RegisterClientScriptBlock(Me.GetType, "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        grv_Master.EditIndex = -1;
        BinData();
    }
    protected void grv_Master_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        clsWebShop cls = new clsWebShop();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("TopicID", grv_Master.DataKeys[e.RowIndex].Value.ToString());
        cls.Del_Topic(msg.GetXmlStr.ToString());
        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Dữ liệu đã cập nhật thành công')</script>");
        BinData();
    }
    protected void grv_Master_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_Master.PageIndex = e.NewPageIndex;
        BinData();
    }
}
